package com.inossem.warehousestorage_inossem.activity.emergency_warehousing;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.MainActivity;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.emergencyWarehousing.ReceiveEmergencyWarehousingListAdapter;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingReceiveListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.emergencyWarehousing.EmergencyWarehousingReceiveListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.List;


/**
 * 作者：wsy on 2017/11/28 10:10
 * <p>
 * 邮箱：445979770@qq.com
 */

public class EmergencyWarehousingListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView receiveEmergencyWarehousingList;

    private List<EmergencyWarehousingReceiveListBean.EmergencyWarehousingListBean> allData;

    private ReceiveEmergencyWarehousingListAdapter adapter;

    private String mReceiptNumber;

    private boolean mFlag = false;
    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.activity_emergency_warehousing_list);
        initView(EmergencyWarehousingListActivity.this);
        initData(EmergencyWarehousingListActivity.this);

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        receiveEmergencyWarehousingList = (PullableListView) activity.findViewById(R.id.receiveEmergencyWarehousingList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.receiveEmergencyWarehousingListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_emergency_warehousing_list_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_main_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getExtra(activity);
        if (mFlag) {
            Intent intent = new Intent(activity, MainActivity.class);
            startActivity(intent);
            finish();
        }else {
            receiveEmergencyWarehousingList.setPullUpFlag(false);
            refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                    initData(activity);
                    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                }

                @Override
                public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                }
            });
            setmScanListener((String scanResult, int length) -> {
                getData(activity, QRCodeUtils.DecryptQRCode(scanResult));
            }, QRCodeConstant.TYPE_URGENT);
            getData(activity, mReceiptNumber);
        }

    }
    private void addEmergencyWarehousingListData(Context context, List<EmergencyWarehousingReceiveListBean.EmergencyWarehousingListBean> receiveEmergencyWarehousingList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ReceiveEmergencyWarehousingListAdapter(context, receiveEmergencyWarehousingList, mReceiptNumber, (String receiptNumber) -> {
                mReceiptNumber = receiptNumber;
                getData(context, mReceiptNumber);
            });
            listView.setAdapter(adapter);
        }
    }
    private void getData(Context context, String condition) {
        EmergencyWarehousingReceiveListRequest.request(context, condition, new GsonBeanListener<EmergencyWarehousingReceiveListBean>() {
            @Override
            public void onSuccess(EmergencyWarehousingReceiveListBean bean) {
                allData = bean.getBody();
                if (allData==null || allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_change_storage_materiel_list_undetected_data));
                }
                mReceiptNumber = condition;
                addEmergencyWarehousingListData(context, ActivityUtils.getShowData(allData, 0), receiveEmergencyWarehousingList, true);
            }
        });
    }
    private void getExtra(Activity activity) {
        mFlag = activity.getIntent().getBooleanExtra(InStorageExtra.FLAG, false);
    }
    public interface EmergencyWarehousingReceiveListener {
        void onNoDoubleClickListener(String receiptNumber);
    }
}
